/**
 * Created by Administrator on 2017/5/16.
 */


var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var ItbBaseModel = require('./ItbBaseModel');

// Schema 结构
var ItbPartnerIncomeModel = function(obj){
    ItbBaseModel.call(this,obj);

    //`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    //`state` int(3) NOT NULL DEFAULT '10' COMMENT '状态: 10新建 20发放失败 30纠纷 40发放成功',
    //`user_id` bigint(20) NOT NULL COMMENT '用户id',
    //`year` int(4) NOT NULL COMMENT '年',
    //`month` int(2) NOT NULL COMMENT '月',
    //`day` int(2) NOT NULL DEFAULT '1' COMMENT '日',
    //`income` bigint(20) NOT NULL DEFAULT '0' COMMENT '收入（单位分）',
    //`desc` text COMMENT '备注',
    //`create_time` datetime NOT NULL,
    //`update_time` datetime NOT NULL,
    //`update_by` varchar(50) NOT NULL DEFAULT '',
    //`del_flg` int(1) NOT NULL DEFAULT '0' COMMENT '删除flg',
};

ItbPartnerIncomeModel.prototype = new ItbBaseModel();

// 取得表名
ItbPartnerIncomeModel.prototype.getTableName = function(){
    return 'itb_partner_income';
};
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
ItbPartnerIncomeModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'id='+this.id;
};

//------------------------------------------------------------------
/* 获取城市合伙人收益记录列表
 * 参数:bForCnt  是否用户获取数据总个数
 * 参数:userId   查询的用户 id
 * 参数:pageNum  查询指定页
 * 参数:pageSize 查询页大小
 * 参数:callback:回调
 * */

function sql_incomeList(bForCnt, userId, pageNum, pageSize){
    var sql = '';
    if(bForCnt) {
        sql += ' SELECT COUNT(*) AS count ';
    } else {
        sql += ' SELECT p.* ';
    }
    sql +=
        " FROM itb_partner_income p, itb_partner t " +
        " WHERE p.del_flg=0 AND p.state=40 " +
        " AND t.del_flg=0 AND t.partner_id=p.partner_id "+
        " AND t.user_id="+userId;

    if(!bForCnt){
        //排序
        var skipCnt = Number(pageNum||'0')*Number(pageSize||'0');
        sql += "  ORDER BY p.year DESC, p.month DESC, p.day DESC, p.create_time desc LIMIT "+skipCnt+", "+Number(pageSize||'0');
    }
    return sql;
}

//------------------------------------------------------------------
/* 获取城市合伙人累计收益记录列表
 * 参数:userId
 * 参数:callback:回调
 * */
ItbPartnerIncomeModel.prototype.getPartnerIncomeList = function(userId, pageNum, pageSize, callback){
    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('ItbPartnerIncomeModel.js [getPartnerIncomeList] Err:'+err);
            return callback(err,null);
        }
        // Use the connection
        var sql = sql_incomeList(false, userId, pageNum, pageSize);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            callback(error, result);
        });
    });
};
//-------------------------------------------------------------------------------
/**
 * 获取城市合伙人累计收益记录项目总个数
 * */
ItbPartnerIncomeModel.prototype.getPartnerIncomeListCnt = function (userId, callback) {
    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('ItbPartnerIncomeModel.js [getPartnerIncomeListCnt] Err:'+err);
            return callback(err,null);
        }
        // Use the connection
        var sql = sql_incomeList(true, userId, null, null);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            if(result && result.length > 0) {
                callback(error, result[0].count);
            }else {
                callback(error, result);
            }
        });
    });
};

//-------------------------------------------------------------------------------
/**
 * 获取城市合伙人累计总收益
 * */
ItbPartnerIncomeModel.prototype.getPartnerTotalIncome = function (userId, callback) {
    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('ItbPartnerIncomeModel.js [getPartnerTotalIncome] Err:'+err);
            return callback(err,null);
        }
        var sql =
            " SELECT  SUM(p.income) AS sum" +
            " FROM itb_partner_income p,itb_partner t " +
            " WHERE p.del_flg=0 AND p.state=40 " +
            " AND t.del_flg=0 AND t.partner_id=p.partner_id "+
            " AND t.user_id="+userId;

        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            callback(error, result);
        });
    });
};




// 导出
module.exports = ItbPartnerIncomeModel;
